const db = require('@/db/db-connection')
const { dateFormat } = require('@/utils/common.util')

class VisitModel {
  /**
   * c端接口访问记录生成
   * @param {*} params
   */
  async add (params) {
    const { ip, province, city, isp, lonlat, path, method } = params
    const date = dateFormat(new Date())
    const sql = 'INSERT INTO visit (ip, province, city, isp, lonlat, path, method, date) VALUES(?, ?, ?, ?, ?, ?, ?, ?)'
    try {
      await db.query(sql, [
        ip,
        province,
        city,
        isp,
        lonlat,
        path,
        method,
        date
      ])
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new VisitModel()
